package besta.moon;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MoonDB {

    private final String dbPath;

    public MoonDB(String path) {
        this.dbPath = path;
    }

    static {
        try {
            Class.forName("besta.moon.JdbcDriver");
        } catch (ClassNotFoundException ex) {
            throw new RuntimeException("must never happen", ex);
        }
    }

    public Connection getConnection() {
        Connection conn;
        try {
            conn = DriverManager.getConnection("jdbc:moon:"+dbPath+"?create=true");
        } catch (SQLException ex) {
            throw new RuntimeException("No database connection", ex);
        }
        return conn;
    }

    public void closeConnection(Connection connection) {
        try {
            connection.close();
        } catch (SQLException sex) {

        }
    }

    public List<Map<String, Object>> queryList(String sql) throws SQLException {
        Connection con = getConnection();
        List list = new ArrayList();
        try (ResultSet rs = con.createStatement().executeQuery(sql)) {
            ResultSetMetaData md = rs.getMetaData();
            int columnCount = md.getColumnCount();
            while (rs.next()) {
                Map rowData = new HashMap();
                for (int i = 1; i <= columnCount; i++) {
                    rowData.put(md.getColumnName(i), rs.getObject(i));
                }
                list.add(rowData);
            }
        }
        return list;
    }

    public Map<String, Object> queryMap(String sql) throws SQLException {
        Connection con = getConnection();
        Map rowData = new HashMap();
        try (ResultSet rs = con.createStatement().executeQuery(sql)) {
            ResultSetMetaData md = rs.getMetaData();
            int columnCount = md.getColumnCount();
            while (rs.next()) {
                for (int i = 1; i <= columnCount; i++) {
                    rowData.put(md.getColumnName(i), rs.getObject(i));
                }
            }
        }

        return rowData;
    }

    public String queryKey(String sql, String key) throws SQLException {
        Connection con = getConnection();
        String res = "";
        try (ResultSet rs = con.createStatement().executeQuery(sql)) {
            while (rs.next()) {
                res = rs.getString(key);
            }
        }
        return res;
    }

    public int execute(String sql) throws SQLException {
        Connection con = getConnection();
        int res = 0;
        try {
            res = con.createStatement().executeUpdate(sql);
        } finally {
            closeConnection(con);
        }
        return res;
    }

    public String queryCount(String table) throws SQLException {
        return queryKey("SELECT COUNT(*) AS C FROM " + table, "C");
    }
}
